package com.ca.ridersclubapplication.view.bar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.util.NumberUtils;

public class MiniScoreLineProcessBar extends ConstraintLayout {

    private String label;
    private float ratio;
    private long value;

    public TextView labelView;
    public MiniLineProcessBar lineProcessBar;
    public  TextView valueView;

    public MiniScoreLineProcessBar(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public MiniScoreLineProcessBar(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public MiniScoreLineProcessBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.mini_score_line_process_bar_layout, this);
        labelView = view.findViewById(R.id.tv_label);
        lineProcessBar = view.findViewById(R.id.bar);
        valueView = view.findViewById(R.id.tv_value);

        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MiniScoreLineProcessBar, 0, 0);

            label = array.getString(R.styleable.MiniScoreLineProcessBar_mslpb_label);
            ratio = array.getFloat(R.styleable.MiniScoreLineProcessBar_mslpb_ratio, 0);
            value = array.getInteger(R.styleable.MiniScoreLineProcessBar_mslpb_value, 0);

            array.recycle();
        }

        setLabel(label);
        setRatio(ratio);
        setValue(value);
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label == null ? "" : label;
        this.labelView.setText(this.label);;
    }

    public float getRatio() {
        return ratio;
    }

    public void setRatio(float ratio) {
        this.ratio = ratio;
        this.lineProcessBar.setRatio(this.ratio);
    }

    public long getValue() {
        return value;
    }

    public void setValue(long value) {
        this.value = value;
        String text = NumberUtils.numberToString(this.value);
        this.valueView.setText(text);
    }
}
